package 反转字符串;

/**
 * @author zhengTao
 * @version 1.0
 * @description: 反转字符串 II
 * @date 2022/10/17 12:30
 */
public class Demo541 {
    public static void main(String[] args) {
        //"abcdef"
        //3
        String abcdefg = reverseStr("abcdef", 3);
        System.out.println(abcdefg);
    }
    public static String reverseStr(String s, int k) {
        StringBuilder newString= new StringBuilder();
        for (int i = 0; i < s.length(); i=i+2*k) {
            int min = Math.min(i + k, s.length());
            String tempStr=s.substring(i, min);
            String s1 = reverseString(tempStr.toCharArray());
            newString= new StringBuilder(newString + s1 + s.substring(min, Math.min(i + 2 * k, s.length())));
        }

        return newString.toString();
    }
    public static String reverseString(char[] s) {

        int left=0;
        int right=s.length-1;
        while (left<right){
            char temp=s[left];
            s[left]=s[right];
            s[right]=temp;
            left++;
            right--;
        }
        return new String(s);
    }


}
